﻿using Microsoft.EntityFrameworkCore;
using XLZF.MicroServices.Models;

namespace XLZF.MicroServices.ModelMigration;

public class XLZFContext : DbContext
{
    /// <summary>
    /// 上下文
    /// </summary>
    public XLZFContext()
    {
        //同时为了除此应用的时候代码能够自动创建数据库可以在EFLearnDbContext里添加，程序运行时保证数据库创建。
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql("server=120.48.37.178;user id=root;password=Aa111111;database=ClothingManage", ServerVersion.Parse("5.6.50-mysql"));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ClothingPicDict>().Property(x => x.ID).ValueGeneratedOnAdd();//设置Id自增

        modelBuilder.Entity<Clothing_Class_Dict>().Property(x => x.ID).ValueGeneratedOnAdd();//设置Id自增

        modelBuilder.Entity<Clothing_Class_Detail>().Property(x => x.ID).ValueGeneratedOnAdd();//设置Id自增

        //一对多
        modelBuilder.Entity<Clothing_Class_Detail>().HasOne(x => x.Clothing_Class_Dict).WithMany(x => x.Details).HasForeignKey(x => x.PID);
    }

    public DbSet<Clothing_Class_Dict> clothing_Class_Dicts { get; set; }

    public DbSet<Clothing_Class_Detail> clothing_Class_Details { get; set; }

    public DbSet<ClothingPicDict> clothingPicDicts { get; set; }

}